package com.gxa.saas.controller;

import com.gxa.saas.exception.BusinessException;
import com.gxa.saas.exception.ExceptionCode;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/sales")
@CrossOrigin
public class AdminController {





    @ApiOperation(value = "登录的方法",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号-测试123",paramType = "query"),
            @ApiImplicitParam(name = "pwd",value = "用户密码-测试123",paramType = "query")
    })

    @PostMapping("/login")
    public Map login(String phone, String pwd, @ApiIgnore HttpSession session) throws Exception {
        //session
        System.out.println("有请求");
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        System.out.println(phone);

        JsonResult jsonResult=new JsonResult();
        if ("123".equals(phone) && "123".equals(pwd)){
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
            jsonResult.set("token",token);
            session.setAttribute("token",token);

        }else{
            throw new BusinessException(ExceptionCode.USER_LOGIN_FAILED);

        }



        return jsonResult.getMap();
    }
}
